package org.uim.cricket.util;

import java.util.ArrayList;

public class MyStringUtils {

	/**
	 * Replace all the occurences of given <code>texts</code> to the 
	 * value of <code>replaceTo</code>.
	 * <p>Each given word in <code>texts</code> will be replaced every where
	 * in the <code>source</code> String.
	 * 
	 * @param source The source string which replacements are to be done.
	 * @param replaceTo The replace text for found words
	 * @param texts The words to be replaced.
	 * @return Modified source string is returned after all the replacements.
	 */
	public static String replaceAll(String source, String replaceTo, String... texts) {
		String result = source;
		for (int i = 0; i < texts.length; i++) {
			result = result.replaceAll(texts[i], replaceTo);
		}
		return result;
	}
	
	public static boolean isNumeric(String text) {
	    if (text == null || text.length() == 0) return false;
		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			if (c == '.' || (c >= '0' && c <= '9')) {
				continue;
			}
			return false;
		}
		return true;
	}

	public static String[] splitInBetween(String content, String fromStr, String toStr) {
		if (content == null) {
			return null;
		}
		int pos = content.indexOf(fromStr);
		if (pos < 0) {
			return null;
		}
		int epos = -1;

		ArrayList<String> result = new ArrayList<String>();
		while (pos >= 0) {
			epos = content.indexOf(toStr, pos + fromStr.length());
			if (epos < 0)
				break;

			result.add(content.substring(pos + fromStr.length(), epos));
			
			pos = content.indexOf(fromStr, epos + toStr.length());
		}
		
		String[] res = new String[result.size()];
		result.toArray(res);
		return res;
	}

	public static String inBetween(String content, String fromStr, String toStr) {
	    if (content == null) {
	        return null;
	    }
	    int pos = content.indexOf(fromStr);
        if (pos < 0) {
            return null;
        }
        int epos = content.indexOf(toStr, pos + fromStr.length());
        
        return content.substring(pos + fromStr.length(), epos);
	}
	
}
